﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace cse480_rtl.Account
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
            lbName.Text = "Name:";
            tbName.Visible = false;
            tbStreetName.Visible = false;
            tbStreetNo.Visible = false;
            tbURL.Visible = false;
            tbZipCode.Visible = false;

            ckDealer.CheckedChanged += new EventHandler(ckDealer_CheckedChanged);
        }

        void ckDealer_CheckedChanged(object sender, EventArgs e)
        {
            if (ckDealer.Checked)
            {
                lbName.Text = "Company Name:";
                tbName.Visible = true;
                tbStreetName.Visible = true;
                tbStreetNo.Visible = true;
                tbURL.Visible = true;
                tbZipCode.Visible = true;
            }
            else
            {
                lbName.Text = "Name:";
                tbName.Visible = false;
                tbStreetName.Visible = false;
                tbStreetNo.Visible = false;
                tbURL.Visible = false;
                tbZipCode.Visible = false;
            }
        }

        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {

            MainDataSet.AllUsersRow allusers = (Master as SiteMaster).DataSet.AllUsers.NewAllUsersRow();
            MainDataSet.NonAdminsRow nonAdmin = (Master as SiteMaster).DataSet.NonAdmins.NewNonAdminsRow();
            MainDataSet.DealerRow dealer = (Master as SiteMaster).DataSet.Dealer.NewDealerRow();

            allusers.LoginID = RegisterUser.UserName;
            allusers.UserPassword = RegisterUser.Password;

            nonAdmin.AllUsers_LoginID = allusers.LoginID;

            dealer.NonAdmins_AllUsers_LoginID = nonAdmin.AllUsers_LoginID;
            dealer.CompanyName = tbName.Text;
            dealer.StreetName = tbStreetName.Text;
           
            dealer.URL = tbURL.Text;
            
            int temp;
            int.TryParse(tbZipCode.Text, out temp);
            dealer.ZipCode = temp;
            int.TryParse(tbStreetNo.Text, out temp);
            dealer.StreetNo = temp;

            (Master as SiteMaster).DataSet.AllUsers.AddAllUsersRow(allusers);
            (Master as SiteMaster).DataSet.NonAdmins.AddNonAdminsRow(nonAdmin);
            (Master as SiteMaster).DataSet.Dealer.AddDealerRow(dealer);


            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);

            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            MainDataSet mainDataSet = (Master as SiteMaster).DataSet;

            MainDataSet.AllUsersRow allusers = mainDataSet.AllUsers.NewAllUsersRow();
            MainDataSet.NonAdminsRow nonAdmin = mainDataSet.NonAdmins.NewNonAdminsRow();
            
            allusers.LoginID = tbUsername.Text;
            allusers.UserPassword = tbPassword.Text;

            nonAdmin.AllUsers_LoginID = allusers.LoginID;
            nonAdmin.City = tbCity.Text;
            nonAdmin.Email = tbEmail.Text;
            nonAdmin.HomeState = tbState.Text;
            nonAdmin.Telephone = tbPhone.Text;

            mainDataSet.AllUsers.AddAllUsersRow(allusers);
            mainDataSet.NonAdmins.AddNonAdminsRow(nonAdmin);

            if (ckDealer.Checked)
            {
                MainDataSet.DealerRow dealer = mainDataSet.Dealer.NewDealerRow();
                dealer.NonAdmins_AllUsers_LoginID = nonAdmin.AllUsers_LoginID;
                dealer.CompanyName = tbName.Text;
                dealer.StreetName = tbStreetName.Text;

                dealer.URL = tbURL.Text;

                int temp;
                int.TryParse(tbZipCode.Text, out temp);
                dealer.ZipCode = temp;
                int.TryParse(tbStreetNo.Text, out temp);
                dealer.StreetNo = temp;

                mainDataSet.Dealer.AddDealerRow(dealer);
            }

            using (SqlConnection connection = new SqlConnection())
            {
                connection.ConnectionString = "Data Source= 35.9.22.104; Initial Catalog=cse480-rtl ;User ID=sa;password= mcppsitb$$";
                connection.Open();

                SqlBulkCopy bulkCopy = new SqlBulkCopy(connection);
                bulkCopy.DestinationTableName = "AllUsers";
                bulkCopy.WriteToServer(mainDataSet.AllUsers);

                bulkCopy.DestinationTableName = "NonAdmins";
                bulkCopy.WriteToServer(mainDataSet.NonAdmins);

                if (ckDealer.Checked)
                { 
                    bulkCopy.DestinationTableName = "Dealer";
                    bulkCopy.WriteToServer(mainDataSet.Dealer);
                }
                bulkCopy.Close();
            }
           
        }
    }
}
